package wawi.kasse;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Hashtable;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import wawi.actions.Verkauf;
import wawi.db.DbConnector;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.Sizes;



public class RechnungWaehlenGUI extends JDialog
{
	private KasseController controller;
	
	public RechnungWaehlenGUI(Frame owner, KasseController controller)
	{
		super(owner);
		initComponents();
		this.controller = controller;
	}

	public RechnungWaehlenGUI(Dialog owner, KasseController controller)
	{
		super(owner);
		initComponents();
		this.controller = controller;
	}

	private void okButtonActionPerformed(ActionEvent e)
	{
		int index = comboBox1.getSelectedIndex();
		int rgNum = index_rgNum.get(index);
		setVisible(false);
		controller.alteRechnungOeffnen(rgNum);		
	}

	private void cancelButtonActionPerformed(ActionEvent e)
	{
		setVisible(false);
	}
	
	
	private Hashtable<Integer, Integer> index_rgNum = new Hashtable<Integer, Integer>();
	private Object index_rgNum_synch = new Object();
	
	public void loadRgData()
	{
		new Thread()
		{
			public void run()
			{
				synchronized (index_rgNum_synch)
				{
					List<Verkauf> list = DbConnector.getInstance().getVerkaufList();
					String[] ret = new String[list.size()];
					index_rgNum = new Hashtable<Integer, Integer>();
					
					for (int i=0; i<list.size(); i++)
					{
						ret[i] = list.get(i).getRgNum();
						index_rgNum.put(i, list.get(i).getRecid());
					}
					
					comboBox1.setModel(new DefaultComboBoxModel(ret));
					comboBox1.setEnabled(true);
					okButton.setEnabled(true);
				}
			}
		}.start();
	}
	
	

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		dialogPane = new JPanel();
		contentPanel = new JPanel();
		label1 = new JLabel();
		comboBox1 = new JComboBox();
		buttonBar = new JPanel();
		okButton = new JButton();
		cancelButton = new JButton();
		CellConstraints cc = new CellConstraints();

		//======== this ========
		setTitle("Rechnung w\u00e4hlen");
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());

		//======== dialogPane ========
		{
			dialogPane.setBorder(Borders.DIALOG_BORDER);
			dialogPane.setLayout(new BorderLayout());

			//======== contentPanel ========
			{
				contentPanel.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
						FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC
					},
					new RowSpec[] {
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.LINE_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC
					}));

				//---- label1 ----
				label1.setText("Bitte w\u00e4hlen Sie eine Rechnung aus dem Archiv");
				contentPanel.add(label1, cc.xywh(1, 1, 3, 1));

				//---- comboBox1 ----
				comboBox1.setEnabled(false);
				contentPanel.add(comboBox1, cc.xywh(1, 5, 5, 1));
			}
			dialogPane.add(contentPanel, BorderLayout.CENTER);

			//======== buttonBar ========
			{
				buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
				buttonBar.setLayout(new FormLayout(
					new ColumnSpec[] {
						FormFactory.GLUE_COLSPEC,
						FormFactory.BUTTON_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.BUTTON_COLSPEC
					},
					RowSpec.decodeSpecs("pref")));

				//---- okButton ----
				okButton.setText("OK");
				okButton.setEnabled(false);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okButtonActionPerformed(e);
					}
				});
				buttonBar.add(okButton, cc.xy(2, 1));

				//---- cancelButton ----
				cancelButton.setText("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelButtonActionPerformed(e);
					}
				});
				buttonBar.add(cancelButton, cc.xy(4, 1));
			}
			dialogPane.add(buttonBar, BorderLayout.SOUTH);
		}
		contentPane.add(dialogPane, BorderLayout.CENTER);
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JPanel dialogPane;
	private JPanel contentPanel;
	private JLabel label1;
	private JComboBox comboBox1;
	private JPanel buttonBar;
	private JButton okButton;
	private JButton cancelButton;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
